#!/bin/bash

# 头条自动发文系统 - Docker部署脚本
# 适用于阿里云服务器 (DNF Linux系统)

set -e

echo "🚀 开始部署头条自动发文系统..."

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 检查是否为root用户
if [ "$EUID" -ne 0 ]; then
    echo -e "${RED}请使用root用户运行此脚本${NC}"
    exit 1
fi

# 1. 更新系统
echo -e "${BLUE}📦 更新系统包...${NC}"
dnf update -y

# 2. 安装Docker
echo -e "${BLUE}🐳 安装Docker...${NC}"
if ! command -v docker &> /dev/null; then
    dnf install -y dnf-plugins-core
    dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
    dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    
    # 启动Docker服务
    systemctl start docker
    systemctl enable docker
    
    echo -e "${GREEN}✅ Docker安装完成${NC}"
else
    echo -e "${YELLOW}⚠️ Docker已安装，跳过安装步骤${NC}"
fi

# 3. 安装Docker Compose (如果没有)
echo -e "${BLUE}🔧 检查Docker Compose...${NC}"
if ! command -v docker-compose &> /dev/null; then
    curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    echo -e "${GREEN}✅ Docker Compose安装完成${NC}"
else
    echo -e "${YELLOW}⚠️ Docker Compose已安装${NC}"
fi

# 4. 创建项目目录
PROJECT_DIR="/opt/toutiao-auto-publish"
echo -e "${BLUE}📁 创建项目目录: ${PROJECT_DIR}${NC}"
mkdir -p ${PROJECT_DIR}
cd ${PROJECT_DIR}

# 5. 创建必要的目录
echo -e "${BLUE}📁 创建数据目录...${NC}"
mkdir -p data logs uploads browser_data

# 6. 设置目录权限
echo -e "${BLUE}🔐 设置目录权限...${NC}"
chmod 755 data logs uploads browser_data
chown -R 1000:1000 data logs uploads browser_data

# 7. 创建环境配置文件
echo -e "${BLUE}⚙️ 创建环境配置文件...${NC}"
cat > .env << EOF
# 应用配置
SECRET_KEY=$(openssl rand -hex 32)
DEBUG=False

# 服务器配置
HOST=0.0.0.0
PORT=8000

# 浏览器配置
BROWSER_HEADLESS=true
BROWSER_NO_SANDBOX=true
BROWSER_DISABLE_GPU=true
BROWSER_DISABLE_DEV_SHM=true

# 日志配置
LOG_LEVEL=INFO
LOG_FILE=/app/logs/app.log

# 文件上传配置
UPLOAD_DIR=/app/uploads
MAX_FILE_SIZE=10485760

# 头条配置
TOUTIAO_LOGIN_URL=https://mp.toutiao.com/auth/page/login/
TOUTIAO_PUBLISH_URL=https://mp.toutiao.com/profile_v4/graphic/publish
TOUTIAO_TITLE_URL=https://mp.toutiao.com/profile_v4/graphic/articles
EOF

echo -e "${GREEN}✅ 环境配置文件创建完成${NC}"

# 8. 配置防火墙
echo -e "${BLUE}🔥 配置防火墙...${NC}"
if command -v firewall-cmd &> /dev/null; then
    firewall-cmd --permanent --add-port=8000/tcp
    firewall-cmd --reload
    echo -e "${GREEN}✅ 防火墙配置完成 (端口8000已开放)${NC}"
else
    echo -e "${YELLOW}⚠️ 未检测到firewall-cmd，请手动开放8000端口${NC}"
fi

# 9. 创建systemd服务文件
echo -e "${BLUE}🔧 创建systemd服务...${NC}"
cat > /etc/systemd/system/toutiao-auto-publish.service << EOF
[Unit]
Description=Toutiao Auto Publish Service
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=${PROJECT_DIR}
ExecStart=/usr/local/bin/docker-compose up -d
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable toutiao-auto-publish

echo -e "${GREEN}✅ systemd服务创建完成${NC}"

# 10. 显示部署信息
echo -e "${GREEN}"
echo "🎉 部署准备完成！"
echo "=================================="
echo "项目目录: ${PROJECT_DIR}"
echo "访问地址: http://your-server-ip:8000"
echo "日志目录: ${PROJECT_DIR}/logs"
echo "数据目录: ${PROJECT_DIR}/data"
echo "=================================="
echo -e "${NC}"

echo -e "${YELLOW}📋 接下来的步骤:${NC}"
echo "1. 将您的项目文件上传到 ${PROJECT_DIR}"
echo "2. 运行: cd ${PROJECT_DIR} && docker-compose up -d"
echo "3. 查看日志: docker-compose logs -f"
echo "4. 停止服务: docker-compose down"
echo ""
echo -e "${BLUE}💡 提示:${NC}"
echo "- 确保阿里云安全组已开放8000端口"
echo "- 首次启动可能需要较长时间下载依赖"
echo "- 可以使用 systemctl start toutiao-auto-publish 启动服务"

echo -e "${GREEN}✅ 部署脚本执行完成！${NC}"
